package com.dz.lm.service.comp.comp.impl;

import com.dz.lm.common.dto.AttachDTO;
import com.dz.lm.common.dto.CompDTO;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.dao.comp.read.CompDao;
import com.dz.lm.dao.product.write.CommonAreaDao;
import com.dz.lm.domain.comp.Comp;
import com.dz.lm.domain.comp.CompExample;
import com.dz.lm.domain.comprole.CompRole;
import com.dz.lm.service.attach.AttachService;
import com.dz.lm.service.comp.comp.CompService;
import com.dz.lm.service.comp.account.CompAccountService;
import com.dz.lm.service.comp.role.CompRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.rowset.serial.SerialException;
import java.util.List;
import java.util.Random;

@Service
public class CompServiceImpl implements CompService {
    @Resource(name = "readCompDao")
    private CompDao readCompDao;
    @Resource(name = "writeCompDao")
    private com.dz.lm.dao.comp.write.CompDao writeCompDao;
    @Autowired
    private CompRoleService compRoleService;
    @Autowired
    private CompAccountService compAccountService;
    @Resource(name = "writeCommonAreaDao")
    private CommonAreaDao writeCommonAreaDao;
    @Autowired
    private AttachService attachService;
    @Override
    public int add(Comp comp) throws ServiceException {
        return writeCompDao.insertSelective(comp);
    }

    @Override
    public int updateCode(Comp comp) throws ServiceException {
        CompExample ex = new CompExample();
        CompExample.Criteria cr = ex.createCriteria();
        cr.andCodeEqualTo(comp.getCode());
        cr.andCompIdNotEqualTo(comp.getCompId());
        List<Comp> comps = readCompDao.selectByExample(ex);
        if (comps != null && comps.size() > 0) {
            return -998;
        }
        return writeCompDao.updateByPrimaryKeySelective(comp);
    }

    @Override
    public Comp get(int compId) throws ServiceException {
        CompDTO dto = null;

        Comp comp = readCompDao.selectByPrimaryKey(compId);
        if(comp != null ){
            dto = new CompDTO();
            BeanUtils.copyProperties(comp,dto);
            List<CompRole> roles = compRoleService.getByComp(comp.getCompId());
            if (roles != null) {
                dto.setRoles(roles);
            }
            if(comp.getVideoId() > 0){
                AttachDTO attach = attachService.sleletById(comp.getVideoId());
                dto.setVideo(attach);
            }
        }
        return dto;
    }

    @Override
    public CompDTO getByCode(String code) throws ServiceException {
        CompDTO comp = null;
        CompExample ex = new CompExample();
        CompExample.Criteria cr = ex.createCriteria();
        cr.andCodeEqualTo(code);
        List<Comp> comps = readCompDao.selectByExample(ex);
        if (comps != null && comps.size() > 0) {
            comp = new CompDTO();
            BeanUtils.copyProperties(comps.get(0), comp);
            List<CompRole> roles = compRoleService.getByComp(comp.getCompId());
            if (roles != null) {
                comp.setRoles(roles);
            }
        }
        return comp;
    }

    @Override
    public String registerComp(Comp comp, String userName, String password) throws ServiceException {
        int ret =0;
            String code =getCode();
            comp.setCode(code);
        int add = this.add(comp);
        if(comp.getCompId() > 0){
            try {
                ret = compAccountService.add(comp.getCompId(), 0, 1, userName, password);
            } catch (SerialException throwables) {
                throwables.printStackTrace();
            }
        }
        if(ret == 0){
            code ="";
        }
        return code;
    }
    private String getCode(){
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random1 = new Random();
        //指定字符串长度，拼接字符并toString
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            //获取指定长度的字符串中任意一个字符的索引值
            int number = random1.nextInt(str.length());
            //根据索引值获取对应的字符
            char charAt = str.charAt(number);
            sb.append(charAt);
        }
        String str1 = sb.toString();
        return str1;
    }
}
